<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>


                    <div class="fieldset1 tabel_col clearFix" id="templateContainer">
                        <div class="caption">
                            <span class="title">问卷</span>
                        </div>
                    </div>


<script type="text/javascript">
    var templateCd = "<%=request.getParameter("templateCd")%>";
    // 业务主键
    var id = "<%=request.getParameter("id")%>";
    $(function () {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{templateCd:templateCd,id:id},
            url:"<%=path%>/template/view.do",
            success:function (data) {
                if(data){
                    genHtml(data);
                }
            }
        })
    });

    /**
     * 生成页面
     * @param data
     *
     */

    function genHtml(data){
            var html = "" ;
        for(var question of data){
            var fieldBox = "<div class=\"fieldBox col_100 \" id=\""+question.id+"\">";
            fieldBox += "<div class=\"field\">" + question.qNum + "." + question.question + "</div>";
            fieldBox += "<div class=\"field col_100 clearFix\">";
            var optionList = question.option;
            var ans = question.answer;
            for(var opt of optionList){
                var optType = 1; //选项类型 1单选 2多选 3文本域
                if(opt.optType){
                    optType = opt.optType
                }
                if(optType== 1){
                    var checked = ""; //是否选中
                    if(opt.id == ans){
                        checked += "checked=\"checked\"";
                    }
                    fieldBox += "<span class=\"col_100 \" name=\"span" + opt.id + "\" id=\"span" + opt.id + "\"><input name=\"" + question.id + "\" value=\""+ opt.id +"\" type=\"radio\" disabled=\"disabled\""+checked+">"+opt.name+" </span>"
                }else if(optType==3){
                    fieldBox += "<span class=\"col_100 \" name=\"span" + opt.id + "\" id=\"span" + opt.id + "\"><input name=\"" + question.id + "\" value=\""+ ans +"\" type=\"text\" disabled=\"disabled\" > </span>"
                }
            }
            fieldBox += "</div>";
            fieldBox += "</div>";
            if(ans){
                html += fieldBox;
            }
        }
        // add
        $("#templateContainer").append(html);
    }

    /**
     * 控制后面的题是否显示
     * @param qList
     */
    function genJS(qList){
        // 答案array
        var ansArr = qList.map(function(q){return q.answer});

        for (question of qList){
            var qId = question.id;

            if(question.hasPre){ //存在前置条件
                $("#field"+qId).hide();
                var preOptList = question.preOptList ;
                for(var preOpt of preOptList){
                    for(var ans of ansArr){
                        if(preOpt.key==ans){ //
                            $("#field"+qId).show();
                        }
                    }
                }

            }
        }
    }
</script>

